package com.aletto.general_store.ui.adapters;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;

import com.aletto.general_store.R;
import com.aletto.general_store.data.model.Goods;
import com.aletto.general_store.data.model.Store;
import com.aletto.general_store.ui.activities.StoreDetailsActivity;
import com.aletto.general_store.utils.XHelper;
import com.aletto.general_store.utils.image.GlideApp;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 店铺
 */
public class StoreGoodsAdapter extends BaseQuickAdapter<Store, BaseViewHolder> {

    public StoreGoodsAdapter() {
        super(R.layout.item_store_goods);

        setOnItemClickListener((adapter, view, position) -> {
            Context context = view.getContext();
            Store item = (Store) adapter.getItem(position);
            context.startActivity(StoreDetailsActivity.newIntent(context, item.getId()));
        });
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, Store item) {

        holder.setText(R.id.tv_store_name, item.getName())
                .setText(R.id.tv_sales_volume, "累计销量" + item.getSales());

//        ((RatingBar) holder.getView(R.id.rating)).setRating(item.get);

        ImageView ivStoreLogo = holder.getView(R.id.iv_store_logo);
        GlideApp.with(ivStoreLogo).load(item.getHead_img()).avatar().into(ivStoreLogo);

        RecyclerView recyclerView = holder.getView(R.id.recycler_view);
        recyclerView.setAdapter(new GoodsAdapter(item.getGoodsInfo()));
    }


    private static class GoodsAdapter extends BaseQuickAdapter<Goods, BaseViewHolder> {

        public GoodsAdapter(List<Goods> list) {
            super(R.layout.item_store_goods_grid, list);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder holder, Goods item) {
            holder.setText(R.id.tv_price_unit, item.getPriceUnit())
                    .setText(R.id.tv_price, XHelper.toPrice(item.getPrice())); //  + " USDT"

            List<String> goodsImg = item.getGoodsImg();
            if (goodsImg != null && !goodsImg.isEmpty()) {
                ImageView ivGoodsCover = holder.getView(R.id.iv_goods_cover);
                GlideApp.with(ivGoodsCover).load(goodsImg.get(0)).normal().into(ivGoodsCover);
            }
        }
    }

}
